package co.fitstart.entity.credit;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.user.User;

/**
 * 
 * @author Ysy
 *
 */
@Entity
@Table(name = "credit")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Credit {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 所属用户*/
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private User user;
    
    /** 累计积分*/
    private int totalCredit;
    
    /** 可用积分*/
    private int availableCredit;
    
    /**
     * default constructor
     */
    public Credit(){
        
    }

    /**
     * 
     * @param user
     */
    public Credit(User user) {
        this.user = user;
        this.totalCredit = 0;
        this.availableCredit = 0;
    }

    /**
     * 
     * @param creditValue
     * @return
     */
    public Credit increaseCredit(int creditValue) {
        this.availableCredit = this.availableCredit  + creditValue;
        this.totalCredit = this.totalCredit + creditValue;
        return this;
    }

    /**
     * 
     * @param creditValue
     * @return
     */
    public Credit useCredit(int creditValue){
        this.availableCredit = this.availableCredit - creditValue;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the totalCredit
     */
    public int getTotalCredit() {
        return totalCredit;
    }

    /**
     * @return the availableCredit
     */
    public int getAvailableCredit() {
        return availableCredit;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param totalCredit the totalCredit to set
     */
    public void setTotalCredit(int totalCredit) {
        this.totalCredit = totalCredit;
    }

    /**
     * @param availableCredit the availableCredit to set
     */
    public void setAvailableCredit(int availableCredit) {
        this.availableCredit = availableCredit;
    }
}
